package proto_wearebatman;

public class AND_Gate extends Gate {

    private int prevInput0, prevInput1;

    public AND_Gate(String name) {
        super(name, 2, 1);

        // Proto Output
        Main.println("AND_Gate created: " + this.name);
    }

    public void operate() {
        if((prevInput0 != this.inputs[0].getData() || prevInput1 != this.inputs[1].getData()) && counter > (this.inputs.length-1)) {
            Main.println("Race Condition in "+this.name+"!");
            return;
        }
        this.outputs[0].setData(this.inputs[0].getData() & this.inputs[1].getData());
        this.prevInput0 = this.inputs[0].getData();
        this.prevInput1 = this.inputs[1].getData();
        
        //Main.println(this.name + ".operate() | Data = " + this.outputs[0].getData());

        this.counter++;
        if(counter < 10)
            for(int i=0; i<this.outputs.length; i++)
                this.wireOutputs[0].operate();
    }

    public void resetCounter() {
        this.counter = 0;
    }
}
